<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:th="http://www.thymeleaf.org">

<head lang='zh'>
    <meta charset='utf-8'>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="renderer" content="webkit"/>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0"/>
    <title>共享文档</title>
    <link rel="icon" type="image/x-icon" href="/luckysheet/img/favicon.ico"/>

    <link rel='stylesheet' href='/luckysheet/lib/plugins/css/pluginsCss.css'/>
    <link rel='stylesheet' href='/luckysheet/lib/plugins/plugins.css'/>
    <link rel='stylesheet' href='/luckysheet/lib/css/luckysheet.css'/>
    <link rel='stylesheet' href='/luckysheet/lib/assets/iconfont/iconfont.css'/>
    <script src="/luckysheet/lib/plugins/js/plugin.js"></script>
    <script src="/luckysheet/lib/luckysheet.umd.js"></script>
    <script src='/luckysheet/js/luckyexcel.umd.js'></script>
    <script src='/luckysheet/js/exceljsMin.js'></script>
    <script src='/luckysheet/js/file_saver_min.js'></script>
    <script src='/luckysheet/js/excelDaochu.js'></script>
      
</head>
<body>
<div id="${wb.option.container}"
     style="margin:0px;padding:0px;position:absolute;width:100%;height:100%;left: 0px;top: 0px;"></div>
</div>
</body>
<script>

 // 定义一个函数用于从 Cookie 中获取指定名称的值
 function getCookie(name) {
            // 获取当前页面的所有 Cookie 信息
            const cookies = document.cookie.split('; ');
            // 遍历所有的 Cookie
            for (let i = 0; i < cookies.length; i++) {
                const cookie = cookies[i].split('=');
                // 如果找到指定名称的 Cookie
                if (cookie[0] === name) {
                    // 返回该 Cookie 的值
                    return cookie[1];
                }
            }
            // 如果未找到指定名称的 Cookie，返回 null
            return "未登录";
        }
    // 调用 getCookie 函数获取 sheetUserInfo 的值
    const userName = getCookie('sheetUserInfo');
    var localurl =  "//" + window.location.host;

    // 配置项
    var options = {
        container: "${wb.option.container}", // 设定DOM容器的id
        title: "${wb.option.title}", // 设定表格名称
        lang: "${wb.option.lang}",
        allowUpdate: true,
        loadUrl: window.location.protocol + localurl + "/load/${wb.id}",
        loadSheetUrl: window.location.protocol + localurl + "/loadSheet/${wb.id}",
        // updateUrl: "ws://"+localurl + "/ws/" + Math.round(Math.random() * 100) + "/${wb.id}",
        updateUrl: "ws://"+localurl + "/ws/" + userName + "/${wb.id}",
        allowEdit: true, //作用：是否允许前台编辑
        showinfobar: true, //作用：是否显示顶部信息栏
        myFolderUrl:  window.location.protocol + localurl , //作用：左上角<返回按钮的链接
        functionButton: `
                <button id="logoutButton" class="btn btn-primary btn-warning" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;">`+'欢迎 ' + userName +`</button>
                <button id="exportButton" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;" onclick="FrontDaoChuExcel()">导出</button>
                <button id="logoutButton" class="btn btn-primary btn-warning" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;" onclick="logout()">退出</button>
                <button id="backButton" class="btn btn-primary btn-info" style=" padding:3px 6px; font-size: 12px;margin-right: 130px;" onclick="goBack()">返回</button>
         `
    }
    
    // <button id="downloadButton" class="btn btn-primary btn-danger" style=" padding:3px 6px; font-size: 12px; margin-right: 10px;" onclick="downExcelData()">下载</button>
    
    // 初始化表格
    luckysheet.create(options)

    // function downExcelData() {
    //     try {
    //     console.log("luckysheet.getAllSheets() = " + JSON.stringify(luckysheet.getAllSheets()))

    //     // 获取文件名称
    //     const fileName = luckysheet.toJson ().title;

    //     var form = document.createElement("form");
    //     form.method = 'post';
    //     form.action = '/equipment/excel/downfile';
    //     form.style = 'display:none';
    //     form.enctype = 'multipart/form-data';

    //     document.body.appendChild(form);

    //     var newElement = document.createElement("textarea");
    //     newElement.setAttribute("type","hidden");
    //     newElement.name = "exceldata";
    //     newElement.value = JSON.stringify(luckysheet.getAllSheets());
    //     form.appendChild(newElement);

    //     // 添加文件名称到表单
    //     var fileNameElement = document.createElement('input');
    //     fileNameElement.type = 'hidden';
    //     fileNameElement.name = 'fileName';
    //     fileNameElement.value = fileName;
    //     form.appendChild(fileNameElement);

    //     form.submit();
    //     // 提交后移除表单元素
    //     document.body.removeChild(form);
    // } catch (error) {
    //     console.error('导出 Excel 数据时出现错误:', error);
    // }
    // }



    // 前端导出Excel
    function  FrontDaoChuExcel() {
        // 获取文件名称
        const fileName = luckysheet.toJson ().title;
        
        const selectedApp ='wps';
        // const selectedApp ='office';
        exportExcelFront(luckysheet.getAllSheets(), fileName, selectedApp).then(() => {
            console.log('Excel 导出成功');
        }).catch((error) => {
            console.error('Excel 导出失败:', error);
        });
    };

        

            

    // 退出登录函数
    function logout() {
        $.ajax({
            type: 'POST',
            url: '/user/logout',
            dataType: 'json',
            cache: false,
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            },
            success: function(response) {
                if (response.code === '0000') { // 假设成功码为 '0000'
                    window.location.href = '/login'; // 跳转到登录页面
                } else {
					console.error('退出登录失败:', response.message);
					window.location.href = '/login'; // 跳转到登录页面
                }
            },
            error: function(error) {
                console.error('请求出错:', error);
				window.location.href = '/login'; // 跳转到登录页面
            }
        });
    }

        // 返回函数
        function goBack() {
            // 返回到上一个页面
            window.history.back();
        }
    
</script>
</html>